package cn.edu.cjw.APMFP.ui;

import cn.edu.cjw.APMFP.Util.ImgUtil;
import cn.edu.cjw.APMFP.ui.component.VFlowLayout;
import cn.edu.cjw.APMFP.ui.panel.*;

import javax.swing.*;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.TreeNode;
import javax.swing.tree.TreePath;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Enumeration;

/**
 * @package cn.edu.cjw.APMFP.ui
 * @className CenterPanel
 * @note 中心面板
 * @author wind
 * @date 2022/3/10 20:02
 */
public class CenterPanel extends JPanel {

    private MainFrame mainFrame;
    private final JPanel leftPanel = new JPanel();

    private final JPanel contentPanel = new JPanel();

    private final CardLayout cardLayout = new CardLayout();
    //个人信息
    private final PersonPanel personPanel = new PersonPanel(this);
    //个人信息修改
    private final PersonUpdatePanel personUpdatePanel = new PersonUpdatePanel(this);
    //全部生产任务
    private final ProductionTaskPanel productionTaskPanel = new ProductionTaskPanel(this, 0);
    //生产中
    private final ProductionTaskPanel production1TaskPanel = new ProductionTaskPanel(this, 1);
    //未审核
    private final ProductionTaskPanel production2TaskPanel = new ProductionTaskPanel(this, 2);
    //已完成
    private final ProductionTaskPanel production3TaskPanel = new ProductionTaskPanel(this, 3);
    //工序管理
    private final WorkStepPanel workStepPanel = new WorkStepPanel(this);
    //成品物料管理
    private final BomPanel bomPanel = new BomPanel(this);
    private final Bom1Panel bom1Panel = new Bom1Panel(this);
    private final Bom2Panel bom2Panel = new Bom2Panel(this);

    //产品
    private final ProductPanel productPanel = new ProductPanel(this);

    //审核任务
    private final CheckPanel checkPanel = new CheckPanel(this);

    //员工面板
    private final UserPanel userPanel = new UserPanel(this);
    //角色面板
    private final RolePanel rolePanel = new RolePanel(this);

    public CenterPanel(MainFrame mainFrame){
        this.mainFrame = mainFrame;
        this.setLayout(new BorderLayout(10, 10));
        leftPanel.setPreferredSize(new Dimension(240, 480));
        leftPanel.setBorder(BorderFactory.createLineBorder(Color.gray, 1));
        this.add(leftPanel, BorderLayout.WEST);

        contentPanel.setPreferredSize(new Dimension(570, 480));
        contentPanel.setLayout(cardLayout);
        contentPanel.add(new JPanel(), "emptyPanel");
        contentPanel.add(personPanel, "personPanel");
        contentPanel.add(personUpdatePanel, "personUpdatePanel");
        contentPanel.add(productionTaskPanel, "productionTaskPanel");
        contentPanel.add(production1TaskPanel, "production1TaskPanel");
        contentPanel.add(production2TaskPanel, "production2TaskPanel");
        contentPanel.add(production3TaskPanel, "production3TaskPanel");
        contentPanel.add(bomPanel, "bomPanel");
        contentPanel.add(bom1Panel, "bom1Panel");
        contentPanel.add(bom2Panel, "bom2Panel");
        contentPanel.add(checkPanel, "checkPanel");
        contentPanel.add(productPanel, "productPanel");
        contentPanel.add(workStepPanel, "workStepPanel");
        contentPanel.add(userPanel, "userPanel");
        contentPanel.add(rolePanel, "rolePanel");
        cardLayout.show(contentPanel, "emptyPanel");
        contentPanel.setBorder(BorderFactory.createLineBorder(Color.gray, 1));
        this.add(contentPanel, BorderLayout.CENTER);

        setMenu1();
    }

    /**
     * 个人信息管理
     */
    public void setMenu1(){
        clearLeftPanel();
        leftPanel.setLayout(new VFlowLayout(FlowLayout.CENTER, 10, 80));
        JLabel label1 = new JLabel("个人信息");
//        label1.setMaximumSize(new Dimension(100, 30));
        label1.setPreferredSize(new Dimension(240, 30));
        label1.setHorizontalAlignment(SwingConstants.CENTER);
        JLabel label2 = new JLabel("修改信息");
        label2.setPreferredSize(new Dimension(240, 30));
        label2.setHorizontalAlignment(SwingConstants.CENTER);
        JLabel label3 = new JLabel("退出登陆");
        label3.setPreferredSize(new Dimension(240, 30));
        label3.setHorizontalAlignment(SwingConstants.CENTER);
        leftPanel.add(label1);
        leftPanel.add(label2);
        leftPanel.add(label3);
        label1.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                selectLabel(label1, true);
                selectLabel(label2, false);
                cardLayout.show(contentPanel, "personPanel");
            }
        });
        label2.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                selectLabel(label1, false);
                selectLabel(label2, true);
                cardLayout.show(contentPanel, "personUpdatePanel");
            }
        });
        label3.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                System.exit(-1);
            }
        });
    }

    /**
     * 生产任务管理
     */
    public void setMenu2(){
        clearLeftPanel();
        DefaultTreeCellRenderer cellRender = new DefaultTreeCellRenderer();
        Image openImage = ImgUtil.getImage("/top.png");
        ImageIcon openIcon = new ImageIcon(openImage.getScaledInstance(30, 30, Image.SCALE_SMOOTH));
        cellRender.setOpenIcon(openIcon);
        Image closeImage = ImgUtil.getImage("/right.png");
        ImageIcon closeIcon = new ImageIcon(closeImage.getScaledInstance(30, 30, Image.SCALE_SMOOTH));
        cellRender.setLeafIcon(new ImageIcon());
        cellRender.setClosedIcon(closeIcon);
        cellRender.setHorizontalAlignment(SwingConstants.CENTER);
        cellRender.setBackgroundNonSelectionColor(this.getBackground());
        cellRender.setBackgroundSelectionColor(Color.lightGray);
        cellRender.setBorderSelectionColor(this.getBackground());
        cellRender.setTextSelectionColor(this.getBackground());

        leftPanel.setLayout(new VFlowLayout(FlowLayout.CENTER, 10, 30));
        JPanel jPanel1 = new JPanel(null);
        jPanel1.setPreferredSize(new Dimension(240, 100));

        DefaultMutableTreeNode rootNode1 = new DefaultMutableTreeNode("生产任务");
        JTree jTree1 = new JTree(rootNode1);

        JLabel label2 = new JLabel("审核任务");
        JLabel label3 = new JLabel("工序管理");
        JLabel label4 = new JLabel("产品管理");

//        jTree1.setPreferredSize(new Dimension(240, 100));
        jTree1.setOpaque(false);
        expandTree(jTree1, new TreePath(rootNode1));
        jTree1.setBackground(this.getBackground());
        jTree1.setCellRenderer(cellRender);
        jTree1.setToggleClickCount(1);
        rootNode1.add(new DefaultMutableTreeNode("所有任务"));
        rootNode1.add(new DefaultMutableTreeNode("生产中任务"));
        rootNode1.add(new DefaultMutableTreeNode("未审核任务"));
        rootNode1.add(new DefaultMutableTreeNode("已完成任务"));
        jTree1.setBounds(50, 0, 150, 100);
        jPanel1.add(jTree1);
        jTree1.addTreeSelectionListener(new TreeSelectionListener() {
            @Override
            public void valueChanged(TreeSelectionEvent e) {
                // 获取被选中的相关节点
                TreePath path = e.getNewLeadSelectionPath();
                String str = path.toString();
                str = str.substring(1, str.length() - 1);
                String[] arr = str.split(" ");
                if(arr.length == 1){
                    unselectTree(jTree1, cellRender);
                }else if(arr.length > 1 && "所有任务".equals(arr[1])){
                    cardLayout.show(contentPanel, "productionTaskPanel");
                }else if(arr.length > 1 && "生产中任务".equals(arr[1])){
                    cardLayout.show(contentPanel, "production1TaskPanel");
                }else if(arr.length > 1 && "未审核任务".equals(arr[1])){
                    cardLayout.show(contentPanel, "production2TaskPanel");
                }else if(arr.length > 1 && "已完成任务".equals(arr[1])){
                    cardLayout.show(contentPanel, "production3TaskPanel");
                }
                selectLabel(label2, false);
                selectLabel(label3, false);
                selectLabel(label4, false);
           }
        });

        JPanel jPanel2 = new JPanel(null);
        jPanel2.setPreferredSize(new Dimension(240, 30));
        label2.setHorizontalAlignment(SwingConstants.CENTER);
        label2.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                unselectTree(jTree1, cellRender);
                selectLabel(label2, true);
                selectLabel(label3, false);
                selectLabel(label4, false);
                unselectTree(jTree1, cellRender);
                cardLayout.show(contentPanel, "checkPanel");
            }
        });
        label2.setBounds(65, 0, 100, 20);
        jPanel2.add(label2);


        JPanel jPanel3 = new JPanel(null);
        jPanel3.setPreferredSize(new Dimension(240, 30));
        label3.setHorizontalAlignment(SwingConstants.CENTER);
        label3.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                unselectTree(jTree1, cellRender);
                selectLabel(label2, false);
                selectLabel(label3, true);
                selectLabel(label4, false);
                unselectTree(jTree1, cellRender);
                cardLayout.show(contentPanel, "workStepPanel");
            }
        });
        label3.setBounds(65, 0, 100, 20);
        jPanel3.add(label3);

        JPanel jPanel4 = new JPanel(null);
        jPanel4.setPreferredSize(new Dimension(240, 30));
        label4.setHorizontalAlignment(SwingConstants.CENTER);
        label4.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                unselectTree(jTree1, cellRender);
                selectLabel(label2, false);
                selectLabel(label3, false);
                selectLabel(label4, true);
                unselectTree(jTree1, cellRender);
                cardLayout.show(contentPanel, "productPanel");
            }
        });
        label4.setBounds(65, 0, 100, 20);
        jPanel4.add(label4);

        leftPanel.add(jPanel1);
        leftPanel.add(jPanel2);
        leftPanel.add(jPanel3);
        leftPanel.add(jPanel4);
    }

    /**
     * 成品物料管理
     */
    public void setMenu3(){
        clearLeftPanel();
        leftPanel.setLayout(new VFlowLayout(FlowLayout.CENTER, 10, 80));
        JLabel label1 = new JLabel("BOM管理");
        label1.setPreferredSize(new Dimension(240, 30));
        label1.setHorizontalAlignment(SwingConstants.CENTER);
        JLabel label2 = new JLabel("成品管理");
        label2.setPreferredSize(new Dimension(240, 30));
        label2.setHorizontalAlignment(SwingConstants.CENTER);
        JLabel label3 = new JLabel("原料管理");
        label3.setPreferredSize(new Dimension(240, 30));
        label3.setHorizontalAlignment(SwingConstants.CENTER);
        leftPanel.add(label1);
        leftPanel.add(label2);
        leftPanel.add(label3);

        label1.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                selectLabel(label1, true);
                selectLabel(label2, false);
                selectLabel(label3, false);
                cardLayout.show(contentPanel, "bomPanel");
            }
        });

        label2.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                selectLabel(label2, true);
                selectLabel(label1, false);
                selectLabel(label3, false);
                cardLayout.show(contentPanel, "bom1Panel");
            }
        });

        label3.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                selectLabel(label3, false);
                selectLabel(label1, false);
                selectLabel(label2, false);
                cardLayout.show(contentPanel, "bom2Panel");
            }
        });
    }

    /**
     * 员工账号管理
     */
    public void setMenu4(){
        clearLeftPanel();
        leftPanel.setLayout(new VFlowLayout(FlowLayout.CENTER, 10, 80));
        JLabel label1 = new JLabel("员工管理");
        label1.setPreferredSize(new Dimension(240, 30));
        label1.setHorizontalAlignment(SwingConstants.CENTER);
        JLabel label2 = new JLabel("角色管理");
        label2.setPreferredSize(new Dimension(240, 30));
        label2.setHorizontalAlignment(SwingConstants.CENTER);
        leftPanel.add(label1);
        leftPanel.add(label2);

        label1.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                selectLabel(label1, true);
                selectLabel(label2, false);
                cardLayout.show(contentPanel, "userPanel");
            }
        });

        label2.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                selectLabel(label2, true);
                selectLabel(label1, false);
                cardLayout.show(contentPanel, "rolePanel");
            }
        });
    }

    /**
     * 审核
     */
    public void setMenu5(){
        clearLeftPanel();
    }

    /**
     * 清楚leftPanel组件
     */
    public void clearLeftPanel(){
        leftPanel.removeAll();
        leftPanel.repaint();
        cardLayout.show(contentPanel, "emptyPanel");
    }

    /**
     * 取消选择
     * @param jTree
     */
    public void unselectTree(JTree jTree, DefaultTreeCellRenderer cellRender){
        try {
            jTree.clearSelection();
            jTree.repaint();
        } catch (Exception ignored) {

        }
    }

    /**
     * 设置标签样式
     * @param label
     * @param isSelected
     */
    public void selectLabel(JLabel label, boolean isSelected){
        label.setOpaque(true);
        label.setForeground(isSelected ? Color.white : Color.BLACK);
        label.setBackground(isSelected ? Color.lightGray : this.getBackground());
    }

    public MainFrame getMainFrame() {
        return mainFrame;
    }

    public void setMainFrame(MainFrame mainFrame) {
        this.mainFrame = mainFrame;
    }

    /**
     * 关闭主窗体
     */
    public void close(){
        if(mainFrame != null){
            mainFrame.dispose();
        }
    }

    private void expandTree(JTree tree, TreePath parent) {
        TreeNode node = (TreeNode) parent.getLastPathComponent();
        if (node.getChildCount() >= 0) {
            for (Enumeration<?> e = node.children(); e.hasMoreElements();) {
                TreeNode n = (TreeNode) e.nextElement();
                TreePath path = parent.pathByAddingChild(n);
                expandTree(tree, path);
            }
        }
        tree.expandPath(parent);
    }
}
